<?
  function uploadStoreData() {
    $errorcodes = array(
      UPLOAD_ERR_INI_SIZE => 'The uploaded file exceeds the upload_max_filesize directive in php.ini.',
      UPLOAD_ERR_FORM_SIZE => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.',
      UPLOAD_ERR_PARTIAL => 'The uploaded file was only partially uploaded.',
      UPLOAD_ERR_NO_FILE => 'No file was uploaded.',
      UPLOAD_ERR_NO_TMP_DIR => 'Missing a temporary folder.',
      UPLOAD_ERR_CANT_WRITE => 'Failed to write file to disk.',
      UPLOAD_ERR_EXTENSION => 'File upload stopped by extension.',
    );

    $error = $_FILES['xlsdata']['error'];

    $_FILES['xlsdata']['error'];
    $target_path = "bin/xlsdata/";
    $target_path = $target_path . basename( $_FILES['xlsdata']['name']);
    if(move_uploaded_file($_FILES['xlsdata']['tmp_name'], $target_path)) {
      $result = "The file ".  basename( $_FILES['xlsdata']['name']).  " has been uploaded.";
    } else{
      $result = "<b>ERROR ($error):</b><br> $errorcodes[$error]";
    }

    return $result;
  }

  function parseStoreData(){
    chdir('bin');
    $perlcmd = Props::get("exec", "perl");
    exec($perlcmd . ' ./StoreDataParser.pl val', $output, $return);

    if ($return != 0) {
      $result = "<font color=\"red\">ERROR: DO NOT PROCEED WITH UPDATE!</font>\n";
      $result .= "An error was encountered during processing.  Please contact the developer before\nproceeding.\n\n";
    } else {
      $result = "The store_data.xls file has been validated.  Please resolve any format errors that\nfollow by editing the spreadsheet and performing all three steps again.\n\n";
    }

    foreach ($output as $line) {
      $result .= $line."\n";
    }

    chdir('..');
    return $result;
  }

  function loadStoreData(){
    $result = "";

    chdir('bin');
    $perlcmd = Props::get("exec", "perl");
    exec($perlcmd . ' ./StoreDataParser.pl sql', $output, $return);
    
    if ($return != 0) {
      $result = "<font color=\"red\">ERROR: DO NOT PROCEED WITH UPDATE!</font>\n";
      $result .= "An error was encountered during processing.  Please contact the developer before\nproceeding.\n\n";
    } else {
      $result = "The store_data.xls file has been loaded.  Please resolve any format errors that\nfollow by editing the spreadsheet and performing all three steps again.\n\n";
    }

    foreach ($output as $line) {
      $result .= $line."\n";
    }

    chdir('..');
    return $result;
  }

  function fileChecks(){
    if (file_exists('bin/xlsdata/store_data.xls')) {
        echo "<img src=\"../images/checkmark_green.png\" width=20 height=18>";
        echo "store_data.xls is ready. ";
        echo "The last upload was ". date("M d, Y h:m:s T",filectime('bin/xlsdata/store_data.xls')) .".<br><br>\n";
    } else {
        echo "<img src=\"../images/x_red.png\" width=20 height=18>";
        echo "store_data.xls has not been uploaded.<br><br>\n"; 
    }
  }

  function dbChecks(){
    $db = new DB();
    $result = $db->setQuery("select count(*) from store");
    $count = $result->fetch_row();

    if ($count[0] > 0) {
        $result = $db->setQuery("select max(updated) from store");
        $updated = $result->fetch_row();
        echo "<img src=\"../images/checkmark_green.png\" width=20 height=18>";
        echo "There are $count[0] store records in the store database.";
        echo "The last update was at ". date("M d, Y h:m:s T", strtotime($updated[0])) ."<br><br>\n";
    } else {
        echo "<img src=\"../images/x_red.png\" width=20 height=18>";
        echo "There are no store records in the store database.<br><br>\n";
    }
  }

?>
